Page({
  data: {
    active: 0,
    list: [{
      icon: 'wap-home-o',
      url: '/pages/index/index',
      text: '首页'
    }, {
      icon: 'apps-o',
      url: '/pages/category/category',
      text: '分类'
    }, {
      icon: 'shopping-cart-o',
      url: '/pages/cart/cart',
      text: '购物车'
    }, {
      icon: 'contact-o',
      url: '/pages/user/user',
      text: '用户'
    }]
  },
  onChange(event) {
    // event.detail 的值为当前选中项的索引
    const index = event.detail
    const url = this.data.list[index].url
    wx.switchTab({url})
    this.setData({ active: index })
  },
  initTabActive() {
    const page = getCurrentPages().pop()
    const index = this.data.list.findIndex(item => item.url === `/${page.route}`)
    this.setData({
      active: index
    })
  }
});